package com.atwisdom.star.core.dsl.basicClass.impl;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.JsonUtil;
import com.atwisdom.star.common.vo.PageInfo;
import com.atwisdom.star.core.dao.pojo.basicClass.MeshLib;
import com.atwisdom.star.core.dao.service.basicClass.MeshLibService;
import com.atwisdom.star.core.dsl.basicClass.MeshLibDslService;
import com.atwisdom.star.core.dsl.util.DslServiceUtil;
import com.atwisdom.star.exception.pojo.BusinessException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class MeshLibDslServiceImpl implements MeshLibDslService {
    private MeshLibService meshLibService;

    /**
     * 添加模型
     * @param jsonObject  接收参数
     * @return
     */
    @Override
    public JSONObject add(JSONObject jsonObject) {
        try {
            MeshLib meshLib = new MeshLib(jsonObject);
            meshLibService.save(meshLib);
            return meshLib.fetchJsonObject();
        } catch (Exception e) {
            throw new BusinessException("999999", "操作失败！", e);
        }
    }


    /**
     * 批量添加
     * @param jsonObjectList  接收参数
     * @return
     */
    @Override
    public List<JSONObject> batchAdd(List<JSONObject> jsonObjectList) {
        try {
            List<MeshLib> config = this.transformActionDataList(jsonObjectList);
            meshLibService.saveBatch(config);
            return JsonUtil.transformToJson(config);
        } catch (Exception e) {
            throw new BusinessException("999999", "操作失败！", e);
        }
    }

    /**
     * 更新
     * @param jsonObject  接收参数
     * @return
     */
    @Override
    public JSONObject update(JSONObject jsonObject) {
        try {
            MeshLib meshLib = new MeshLib(jsonObject);
            meshLibService.updateById(meshLib);
            return meshLib.fetchJsonObject();
        } catch (Exception e) {
            throw new BusinessException("999999", "操作失败！", e);
        }
    }

    @Override
    public JSONObject updateById(MeshLib meshLib){
         meshLibService.updateById(meshLib);
         return meshLib.fetchJsonObject();
    }

    /**
     * 批量更新
     * @param jsonObjectList  接收参数
     * @return
     */
    @Override
    public List<JSONObject> batchUpdate(List<JSONObject> jsonObjectList) {
        try {
            List<MeshLib> config = this.transformActionDataList(jsonObjectList);
            boolean b = meshLibService.updateBatchById(config);
            return JsonUtil.transformToJson(config);
        } catch (Exception e) {
            throw new BusinessException("999999", "操作失败！", e);
        }
    }

    public boolean deleteById(Long id){
        Integer delCount = meshLibService.getBaseMapper().deleteById(id);
        if(delCount >= 1){
            return true;
        } else {
            throw new BusinessException("999999", "按id删除失败！");
        }
    }

    /**
     * 根据id数组进行删除
     * @param ids 参数
     * @return 返回结果
     */
    @Override
    public boolean deleteByIds(List<Long> ids) {
        Integer delCount = meshLibService.getBaseMapper().deleteBatchIds(ids);
        if(delCount >= 1){
            return true;
        } else {
            throw new BusinessException("999999", "按ids删除失败！");
        }
    }

    /**
     * 不分页查询
     * @return
     */
    @Override
    public List<JSONObject> query(String condition, String orderBy){
        condition = DslServiceUtil.transformOperator(condition);
        QueryWrapper<MeshLib> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(StringUtils.isNotBlank(condition), condition);
        DslServiceUtil.buildOrderBy(queryWrapper, orderBy, "updateTime");
        List<MeshLib> list = meshLibService.list(queryWrapper);
        return JsonUtil.transformToJson(list);
    }

    /**
     * 不分页查询
     * @param condition  接收参数
     * @return
     */
    @Override
    public List<JSONObject> queryInfo(String condition, String orderBy) {
        condition = DslServiceUtil.transformOperator(condition);
        QueryWrapper<MeshLib> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(StringUtils.isNotBlank(condition), condition);
        queryWrapper.select(MeshLib.fetchSimpleColumns());
        DslServiceUtil.buildOrderBy(queryWrapper, orderBy, "updateTime");
        List<MeshLib> list = meshLibService.list(queryWrapper);
        return JsonUtil.transformToJson(list);
    }

    /**
     * 根据id数组进行简单查询
     *
     * @param ids 参数
     * @return 返回值
     */
    @Override
    public List<JSONObject> queryByIds(List<Long> ids) throws BusinessException {
        List<MeshLib> MeshLibs = meshLibService.listByIds(ids);
        return JsonUtil.transformToJson(MeshLibs);
    }

    public MeshLib queryById(Long id) throws BusinessException{
       if(id!=null){
           return meshLibService.getById(id);
       }else{
           throw new BusinessException("999999", "按id为空，查询操作失败！");
       }
    }

    public MeshLib queryByName(String name) throws BusinessException{
        if(StringUtils.isNotBlank(name)){
            LambdaQueryWrapper<MeshLib> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(MeshLib::getName,name);
            List<MeshLib> list = meshLibService.list(wrapper);
            if(list.size() >0){
                return list.get(0);
            }else{
                return null;
            }
        }else{
            throw new BusinessException("999999", "name为空，查询操作失败！");
        }
    }

    /**
     * 分页查询，去除复杂字段
     * @return 返回统一结果类型(ResultInfo)
     */
    @Override
    public PageInfo queryByPage(int pageNumber, int pageSize, String condition, String orderBy) {
        Page<MeshLib> pageInfo = new Page<>(pageNumber, pageSize);
        QueryWrapper<MeshLib> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(StringUtils.isNotBlank(condition), condition);
        DslServiceUtil.buildOrderBy(queryWrapper, orderBy, "updateTime");
        return getPageInfoResult(pageInfo, queryWrapper);
    }


    /**
     * 分页查询，去除复杂字段
     * @return
     */
    @Override
    public PageInfo queryInfoByPage(int pageNumber, int pageSize, String condition, String orderBy) {
        Page<MeshLib> pageInfo = new Page<>(pageNumber, pageSize);
        QueryWrapper<MeshLib> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(StringUtils.isNotBlank(condition), condition);
        queryWrapper.select(MeshLib.fetchSimpleColumns());
        DslServiceUtil.buildOrderBy(queryWrapper, orderBy, "updateTime");
        return getPageInfoResult(pageInfo, queryWrapper);
    }

    public PageInfo getPageInfoResult(Page<MeshLib> pageInfo, QueryWrapper<MeshLib> queryWrapper) {
        Page<MeshLib> queryPage = meshLibService.page(pageInfo, queryWrapper);
        List<MeshLib> records = queryPage.getRecords();
        List<JSONObject> jsonObjectList = getMeshLibJsonObjectList(records);
        Page<JSONObject> queryPageDTO = new Page<>();
        BeanUtils.copyProperties(queryPage, queryPageDTO);
        queryPageDTO.setRecords(jsonObjectList);
        PageInfo pageResult = PageInfo.of(queryPageDTO);
        return pageResult;
    }

    /**
     * 将模型列表转换为jsonObject 列表
     *
     * @param config 模型列表
     * @return 返回结果
     */
    public List<JSONObject> getMeshLibJsonObjectList(List<MeshLib> config) {
        List<JSONObject> jsonList = new ArrayList<>();
        config.forEach(m -> {
            jsonList.add(m.fetchJsonObject());
        });
        return jsonList;
    }


    /**
     * 从actionData 取出jsonObject列表 转换成模型列表
     * @param jsonObjectList 参数
     * @return 返回模型实体集合
     */
    private List<MeshLib> transformActionDataList(List<JSONObject> jsonObjectList) {
        List<MeshLib> config = new ArrayList<>();
        jsonObjectList.forEach(j -> {
            MeshLib meshLib = new MeshLib(j);
            config.add(meshLib);
        });
        return config;
    }
}
